package com.solidtrust.util;

import java.net.HttpURLConnection;
import java.util.List;
import java.util.Random;

import org.apache.http.Header;
import org.apache.http.client.CookieStore;
import org.apache.http.client.config.CookieSpecs;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.log4j.Logger;

public class HttpUtil {
	
	private static final Logger log4j = Logger.getLogger(HttpUtil.class);
	private static final String USER_AGENT = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36";
	private static CloseableHttpClient httpClient;
	private static CookieStore cookieStore;
	
	public static final String UTF_8 = "UTF-8";
	private static Random random = new Random();
	
	public static CloseableHttpClient getHttpClient(){
		if(httpClient == null){
		    RequestConfig requestConfig = RequestConfig.custom()
					.setCircularRedirectsAllowed(true)
					.setCookieSpec("easy")
					.setSocketTimeout(1000 * 60)
					.setConnectTimeout(1000 * 60).build();
		    
		    cookieStore = new BasicCookieStore();
			httpClient = HttpClients.custom()
					.setDefaultRequestConfig(requestConfig)
					.setDefaultCookieStore(cookieStore)
					.build();
		}
		return httpClient;
	}
	
	public static void refreshCookie(){
		cookieStore.clear();;
	}
	
	public static void addHttpHeads(HttpRequestBase request, String host){
		request.addHeader("User-Agent", USER_AGENT);
		request.addHeader("Accept-Language", "zh-CN,zh;q=0.8,und;q=0.6");
		request.addHeader("Host", host);
		request.addHeader("Cache-Control", "max-age=0");
		request.addHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		request.addHeader("Connection", "keep-alive");
		request.addHeader("Accept-Encoding", "gzip, deflate, sdch");
	}
	
	public static void addHttpHeads(HttpURLConnection conn){
		conn.addRequestProperty("User-Agent", USER_AGENT);
		conn.addRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,und;q=0.6");
		conn.addRequestProperty("Host", "www.renrenche.com");
		conn.addRequestProperty("Cache-Control", "max-age=0");
		conn.addRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		conn.addRequestProperty("Connection", "keep-alive");
		conn.addRequestProperty("Accept-Encoding", "gzip, deflate, sdch");
	}

	public static void sleep(){
		try {
			Thread.sleep((random.nextInt(10)) * 1000);
		} catch (InterruptedException e1) {}
	}
	
	public static void printCookie(){
		List<Cookie> list = cookieStore.getCookies();
		for (Cookie c : list) {
			log4j.info(c);
		}
	}
	
	public static void printHeaders(Header[] headers){
		for (Header header : headers) {
			log4j.info(header);
		}
	}
}
